<?php 
namespace app\index\controller;

class Text {
	public function index() {
		$text_id = input('get.text_id');
		if ($text_id != null) {
			$ctext = model('Text');
			$text_data = $ctext::where('id', $text_id)->find();

			$creply = model('Reply');
			$replys_data = $creply::where('tid', $text_id)->order('create_time desc')->select();

			if ($text_data != null) {
				return view('index/text', [
							'text' => $text_data,
							'replys' => $replys_data
						]);
			}
		}
	}

	public function addReply() {
		$text_reply = input('post.content');
		$text_id = input('get.text_id');
		$if_at = input('post.if_at');
		$at_list = input('post.at_list/a');
		$user_id = input('session.user_id');
		if ($text_reply != null and $text_id != null and $user_id != null) {
			$creply = model('Reply');
			$ctext = model('Text');
			$text_data = $ctext::where('id', $text_id)->find();
			$tuser_id = $text_data->uid;
			$reply_data = $creply::where('tid', $text_id)->where('uid', $user_id)->select();
			$if_same = false;
			if ($reply_data != null) {
				foreach ($reply_data as $reply) {
					if($reply->content == $text_reply) {
						$if_same = true;
					}
				}
			}

			if(!$if_same) {
				$creply->data([
					'tid' => $text_id,
					'uid' => $user_id,
					'content' => $text_reply
				]);
				$creply->save();
				$this->updateHeat($tuser_id, 1, 5);
				$this->updateTextReplyNum($text_id);
				if ($user_id != $tuser_id) {
					$this->addMessageList($creply->id, $text_id, 3);
				}
				
				if ($if_at == 'true') {
					$this->addAtList($at_list, $creply->id);
				}

				return json([
	                'status' => 'success',
	                'message' => '提交成功！',
	                'data' =>  null
	            ]);
			}
			else {
				return json([
	                'status' => 'error',
	                'message' => '不能发布重复内容！',
	                'data' =>  null
	            ]);
			}		
		}
		else {
			return json([
	                'status' => 'error',
	                'message' => '发表失败，请重试！',
	                'data' =>  null
	            ]);
		}
	}

	public function addLike() {
		$text_id = input("post.text_id");
		$user_id = input('session.user_id');
		if ($user_id == null) {
			return json([
                'status' => 'error',
                'message' => '未登录，请重试！',
                'data' =>  null
            ]);
		}

		if ($text_id) {
			$clike = model('Like');
			$ctext = model('Text');

			$like_data = $clike::where('uid', $user_id)->where('tid', $text_id)->find();
			if ($like_data) {
				if ($like_data->if_like == 0) {
					$clike->save([
	                    'if_like' => 1
	                ], ['tid' => $text_id, 'uid' => $user_id]);	                
				}
			}
			else {
				$clike->data([
					'uid' => $user_id,
					'tid' => $text_id,
					'type' => 1,
					'if_like' => 1
				]);
				$clike->save();
				$this->addMessageList($clike->id, $text_id, 4);
			}
			$this->updateHeat($text_id, 1, 2);
			$like_num = $clike::where('tid', $text_id)->where('if_like', 1)->count();
			$ctext->save([
				'like_num' => $like_num
			], ['id' => $text_id]);
			return json([
                'status' => 'success',
                'message' => '点赞成功！',
                'data' =>  null
            ]);
		}
		else {
			return json([
                'status' => 'error',
                'message' => '点赞失败，请重试！',
                'data' =>  null
            ]);
		}
	}

	public function deleteLike() {
		$text_id = input("post.text_id");
		$user_id = input('session.user_id');
		if ($user_id == null) {
			return json([
                'status' => 'error',
                'message' => '未登录，请重试！',
                'data' =>  null
            ]);
		}

		if ($text_id) {
			$clike = model('Like');
			$ctext = model('Text');
			$like_data = $clike::where('uid', $user_id)->where('tid', $text_id)->find();
			if ($like_data) {
				if ($like_data->if_like == 1) {
					$clike->save([
	                    'if_like' => 0
	                ], ['tid' => $text_id, 'uid' => $user_id]);	                
				}
				$like_num = $clike::where('tid', $text_id)->where('if_like', 1)->count();
				$ctext->save([
					'like_num' => $like_num
				], ['id' => $text_id]);
				$this->updateHeat($text_id, -1, 2);
				return json([
	                'status' => 'success',
	                'message' => '取赞成功！',
	                'data' =>  null
	            ]);
			}
			else {
				return json([
	                'status' => 'error',
	                'message' => '取赞错误！',
	                'data' =>  null
	            ]);
			}
		}
		else {
			return json([
                'status' => 'error',
                'message' => '点赞失败，请重试！',
                'data' =>  null
            ]);
		}

	}

	private function updateTextReplyNum($text_id) {
		$creply = model('Reply');
		$reply_num = $creply::where('tid', $text_id)->count();
		$ctext = model('Text');
		$ctext->save([
		    'reply_num' => $reply_num
		], ['id' => $text_id]);;
	}

	private function addAtList($at_list, $sender_id) {
		$cmsglist = model('Msglist');
		$data_list = array();
		if ($at_list) {
			foreach ($at_list as $at) {
				$data = array();
				$data['sender'] = $sender_id;
				$data['receiver'] = $at;
				$data['type'] = 2;
				array_push($data_list, $data);
			}
			$cmsglist->isUpdate(false)->saveAll($data_list);
		}
	}

	// receiver_id 对于内容的user_id, sender_id对应回复的id
	private function addMessageList($sender_id, $text_id, $type) {
		$ctext = model('Text');
		$text_data = $ctext::where('id', $text_id)->field('uid')->find();
		$cmsglist = model('Msglist');
		$cmsglist->data([
			'sender' => $sender_id,
			'receiver' => $text_data->uid,
			'type' => $type
		]);
		$cmsglist->save();
	}

	private function updateHeat($user_id, $type, $step) {
		$cuser = model('User');
		if ($type == 1) {
			$cuser::where('id', $user_id)->setInc('heat', $step);
		}
		else {
			$cuser::where('id', $user_id)->setDec('heat', $step);
		}
	}
}